package edu.sandau.b230151.b23015128.controller;

import edu.sandau.b230151.b23015128.model.Goods;
import edu.sandau.b230151.b23015128.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.UUID;
@Controller
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @RequestMapping("list")
    public String list(ModelMap map, Goods goods){
        if ("".equals(goods.getName()))
        {
            goods.setName(null);
        }
        map.put("goodslist",goodsService.finAll(goods));
        return "goods/list";
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable String id){
        goodsService.deleteById(id);
        return "redirect:/goods/list";
    }

    @RequestMapping("create")
    public String create(){
        return "goods/create";
    }

    @RequestMapping("save")
    public String save(Goods s) {
        if(s.getId() == null){
            s.setId(UUID.randomUUID().toString());
        }
        goodsService.save(s);
        return "redirect:/goods/list";
    }

    @RequestMapping("update/{id}")
    public String update(@PathVariable String id,ModelMap map) {
        Goods s =goodsService.getReferenceById(id);
        map.put("goods",s);
        return "goods/update";
    }
}
